package dados;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import basicas.Participante;
import intefaces.InterfaceParticipante;

public class DadosParticipante extends MySQLConnector implements InterfaceParticipante {

	@Override
	public void inserir(Participante part) throws Exception {
		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "INSERT INTO Administrador (nome, email, senha) VALUES (?, ?, ?)";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setString(1, part.getNome());
			preparedStatementInsert.setString(2, part.getEmail());
			preparedStatementInsert.setString(3, part.getSenha());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}

	}

	@Override
	public void alterar(Participante part) throws Exception {
		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "UPDATE Administrador SET nome = ?, email = ?, senha = ? WHERE id_participante = ?";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setString(1, part.getNome());
			preparedStatementInsert.setString(2, part.getEmail());
			preparedStatementInsert.setString(3, part.getSenha());
			preparedStatementInsert.setInt(4, part.getId_participante());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}

	}

	@Override
	public void excluir(Participante part) throws Exception {
		try{
			dbConn = dbConn();
			dbConn.setAutoCommit(false);
	
			String sql = "DELETE FROM Administrador WHERE id_participante = ?";
			
			preparedStatementInsert = dbConn.prepareStatement(sql);
			preparedStatementInsert.setInt(1, part.getId_participante());
			preparedStatementInsert.executeUpdate();
			
			dbConn.commit();
			
		}catch(SQLException ex){
			dbConn.rollback();
		}finally{
			if(preparedStatementInsert != null){
				preparedStatementInsert.close();
			}
			if(dbConn != null){
				dbConn.close();
			}	
		}

	}

	@Override
	public List<Participante> listarTodos() throws Exception {
		List<Participante> lista = new ArrayList<Participante>();
		try{
			logar();
			String sql = "SELECT id_participante, nome, email, senha FROM Participante";
			ResultSet rs = logar().executeQuery(sql);
			while(rs.next()){
				Participante part = new Participante();
				part.setId_participante(rs.getInt("id_prticipante"));
				part.setNome(rs.getString("nome"));
				part.setEmail(rs.getString("email"));
				part.setSenha(rs.getString("senha"));
				lista.add(part);
			}
			deslogar();
			return lista;
		}catch(Exception ex){
			throw new Exception(ex.getMessage());
		}
	}


}
